package com.elite.groovy.kh;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.FileReader;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @projectName: ngsapitask-hst
 * @package: cn.com.kh
 * @className: LogAnalyticJSSTrail
 * @author: LiJT
 * @description: TODO
 * @date: 2023-07-17 18:05
 * @version: 1.0
 */
public class LogChatSQL {
    private static final Logger log = LoggerFactory.getLogger(LogChatSQL.class);

    public static void main(String[] args) throws Exception {

        // 日志文件路径
        String logDir = "E:\\wx_webuser.txt";
        log.debug("♻文件路径:" + logDir);

        loadFileDeal(logDir);

    }

    // 加载并读取文件-核心
    public static void loadFileDeal(String logDir) {

        // 读取日志 - 调用时间 方法 结束时间 耗时
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(logDir))) {
            String line;
            StringBuffer para_sqls = new StringBuffer();

            while ((line = bufferedReader.readLine()) != null) {

                System.out.println(line);

                line = line.replace("http://ccc.davco.cn/dmt/ChatClient/IndexNew?", "");

                String[] params = line.split("&");
                Map<String, String> param_Map = new HashMap<>();
                for (String param : params) {
                    param_Map.put(param.split("=")[0], param.split("=").length>1?param.split("=")[1]:"");
                }

                String Rec_Guid = param_Map.getOrDefault("userId", "");
                String Device = param_Map.getOrDefault("pc", "");
                String Com = param_Map.getOrDefault("com", "");
                String Id = param_Map.getOrDefault("id", "");
                String Userid = param_Map.getOrDefault("userId", "");
                String Username = URLDecoder.decode(param_Map.getOrDefault("userName", ""));
                String Usermobile = param_Map.getOrDefault("userMobile", "");
                String Sourcesys = param_Map.getOrDefault("sourcesys", "");
                String Channeltype = param_Map.getOrDefault("channelType", "");
                String Province = URLDecoder.decode(param_Map.getOrDefault("province", ""));
                String City = URLDecoder.decode(param_Map.getOrDefault("city", ""));
                String Finaorgid = param_Map.getOrDefault("finaorgid", "");
                String Region = URLDecoder.decode(param_Map.getOrDefault("region", ""));
                String Organization = URLDecoder.decode(param_Map.getOrDefault("organization", ""));
                String Salesterritory = URLDecoder.decode(param_Map.getOrDefault("salesterritory", ""));
                String Ip = param_Map.getOrDefault("userId", "");
                String Status = "0";
                String Update_By = "SELITE";
                String To_Queue_Info = "上线迁移";

                String SQL = "Insert Ignore Into Dg_Chat_Inparams (Rec_Guid, Device, Com, Id, Userid, Username, Usermobile, Sourcesys, Channeltype, Province, City, Finaorgid, Region, Organization, Salesterritory, Ip, Status, Update_By, Update_Time, To_Queue_Info) Values ('" + Rec_Guid + "', '" + Device + "', '" + Com + "', '" + Id + "', '" + Userid + "', '" + Username + "', '" + Usermobile + "', '" + Sourcesys + "', '" + Channeltype + "', '" + Province + "', '" + City + "', '" + Finaorgid + "', '" + Region + "', '" + Organization + "', '" + Salesterritory + "', '" + Ip + "', '" + Status + "', '" + Update_By + "', now(), '" + To_Queue_Info + "');";

                para_sqls.append(SQL).append(System.lineSeparator());
            }

            System.out.println(para_sqls);

            KHFileUtil.writeFile(para_sqls.toString(),"E:\\SQL22.txt");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

}
